package serverMgr

import (
	"fmt"
	"src/gcommon"
	"src/zeus/iserver"
	"strings"

	"github.com/buguang01/util"
)

const (
	PATH_INFO    = "info"    //服务器信息
	PATH_LOAD    = "load"    //服务器负载
	PATH_UserNum = "userNum" //在线人数
)

// 获取注册路径
func GetPath(groupid uint32, stype iserver.ServerType2, sid uint64) string {
	return fmt.Sprintf("src/%d/%d/%d/info", stype, groupid, sid)
}

// 指定分组和类型的监听
func GetWatchPath(groupid uint32, stype iserver.ServerType2) string {
	return fmt.Sprintf("src/%d/%d/", stype, groupid)
}

// 获取服务器负载路径
func GetPathLoad(groupid uint32, stype iserver.ServerType2, sid uint64) string {
	return fmt.Sprintf("src/%d/%d/%d/load", stype, groupid, sid)
}

// 折路径 SplitPathServerInfo
func GetPathServerInfo(pathname string) (groupid uint32, stype iserver.ServerType2, sid uint64) {
	sli := strings.Split(pathname, "/")
	if len(sli) >= 4 {
		return uint32(util.NewString(sli[2]).ToIntV()), iserver.ServerType2(util.NewString(sli[1]).ToIntV()), uint64(util.NewString(sli[3]).ToIntV())
	}
	return 0, 0, 0
}

// 根据类型监听服务器
func GetWatchByType(serverType iserver.ServerType2) string {
	return fmt.Sprintf("src/%d/", serverType)
}

// login监听gateway
func GetWatchGateWayPath() string {
	return fmt.Sprintf("src/%d/", iserver.ServerTypeGateway)
}

type UserNumValue struct {
	RobotNum  int32
	UserNum   int32
	OnlineNum int32 `json:",omitempty"`
}

// Lobby的在线人数路径 ,用于写入在线人数
func GetUserNumPath(groupid uint32, stype iserver.ServerType, sid uint64) string {
	if stype == iserver.ServerTypeGateway {
		return fmt.Sprintf("userNum/gate/%d/%d/", groupid, sid)
	}
	return fmt.Sprintf("userNum/lobby/%d/%d/", groupid, sid)
}
func GetWatchLobbyUserNumPath(groupid uint32) string {
	return fmt.Sprintf("userNum/lobby/%d/", groupid) //, iserver.ServerTypeLobby
}
func GetWatchGateUserNumPath(groupid uint32) string {
	return fmt.Sprintf("userNum/gate/%d/", groupid) //, iserver.ServerTypeGateway
}

// 获取Lobby的在线人数路径 ,用于读取在线人数
func SplitLobbyUserKeyParam(pathname string) (groupid uint32, stype iserver.ServerType2, sid uint64) {
	sli := strings.Split(pathname, "/")
	if len(sli) >= 4 {
		return uint32(util.NewString(sli[2]).ToIntV()), gcommon.ServerTypeLobby, uint64(util.NewString(sli[3]).ToIntV())
	}
	return 0, gcommon.ServerTypeLobby, 0
}

// 获取网关的在线人数路径 ,用于读取在线人数
func SplitGateUserKeyParam(pathname string) (groupid uint32, stype iserver.ServerType2, sid uint64) {
	sli := strings.Split(pathname, "/")
	if len(sli) >= 4 {
		return uint32(util.NewString(sli[2]).ToIntV()), iserver.ServerTypeGateway, uint64(util.NewString(sli[3]).ToIntV())
	}
	return 0, iserver.ServerTypeGateway, 0
}
